/**
 * https://leetcode.cn/problems/reverse-linked-list/
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) return head;
        return dfs(head);
    }
    //将链表看成一棵树（只有一个分支）
    //后序遍历
    public ListNode dfs(ListNode head) {
        if(head.next == null) return head;
        ListNode newHead = dfs(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}